#include "changepersonalinfo.h"
#include "ui_changepersonalinfo.h"
#include "logonwidget.h"
#include "user.h"
#include "friendlist.h"
#include <QHostAddress>
#include <QMessageBox>
changepersonalinfo::changepersonalinfo(QWidget *parent,user userSelf) :
    QDialog(parent),
    ui(new Ui::changepersonalinfo)
{
    ui->setupUi(this);
    this->userSelf = userSelf;
    groupButton1=new QButtonGroup(this);
    groupButton1->addButton(ui->gender_0,0);
    groupButton1->addButton(ui->gender_1,1);
    connect(ui->gender_0,SIGNAL(clicked(bool)),this,SLOT(slots_gender()));
    connect(ui->gender_1,SIGNAL(clicked(bool)),this,SLOT(slots_gender()));
}

changepersonalinfo::~changepersonalinfo()
{
    delete ui;

}
void changepersonalinfo::on_btn_change_clicked()
{
    //获取输入的信息
    QString regUsername = ui->reg_username_lineEdit->text();
    if(regUsername=="")
        regUsername=userSelf.getUserName();
    QString regUserqq=ui->reg_userqq_lineEdit->text();//未完成
    if(regUserqq=="")
    {
        if(userSelf.getUserQQ()=="")
            regUserqq="default_qq";
        else
            regUserqq=userSelf.getUserQQ();
    }
    QString regUserBirthDay=ui->reg_userbirthday_lineEdit->text();//未完成
    if(regUserBirthDay=="")
    {
        if(userSelf.getUserBirthday()=="")
            regUserBirthDay="default_birthday";
        else
            regUserBirthDay=userSelf.getUserBirthday();
    }
    QString regUserEmail=ui->reg_useremail_lineEdit->text();//未完成
    if(regUserEmail=="")
    {
        if(userSelf.getUserEmail()=="")
            regUserEmail="default_email";
        else
            regUserEmail=userSelf.getUserEmail();
    }
    QString regUserPhoto=ui->reg_userphoto_lineEdit->text();//未完成
    if(regUserPhoto=="")
    {
        if(userSelf.getUserPhoto()=="")
            regUserPhoto="default_photo";
        else
            regUserPhoto=userSelf.getUserPhoto();
    }
    QString senderToken = userSelf.getUserPassword();
    if(gender=="-1")
        gender=userSelf.getUserGender();
    user Sender;
    //建立新的修改信息
    QJsonObject new_info_value{
        {"id",userSelf.getUserId().toInt()},
        {"username", regUsername},
        {"photo", regUserPhoto},
        {"qq",regUserqq},
        {"birthday",regUserBirthDay},
        {"password", "kk"},
        {"email",regUserEmail},
        {"gender",gender.toInt()}
    };
    qDebug()<<"SenderValue"<<new_info_value;


    QString resourcePath = ":/asset/config/config.json";
    QFile jsonFile(resourcePath);
    if (jsonFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    QByteArray jsonData = jsonFile.readAll();
    // 将 JSON 数据转换为 QVariant 对象
    QJsonParseError error;
    QJsonObject result = QJsonDocument::fromJson(jsonData, &error).object();
    QJsonObject jsonUrl = QJsonDocument::fromJson(jsonData).object();
    qDebug() << "url :" << jsonUrl;
    jsonFile.close();
    QVariantMap urlVariantMap = jsonUrl.toVariantMap();
    QMap urlMap = urlVariantMap.value("server").toMap();
    host = urlMap.value("host").toString();
    port = urlMap.value("port").toString();
    }
    QNetworkRequest request(QUrl("http://"+host+':'+port+"/slimarveous/set_profile"));//连接后端set_profile文件



    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json2");//获取http协议请求
    QJsonObject json2{//传到后端的json文件
        {"token",senderToken},
        {"id",userSelf.getUserId().toInt()},
        {"new_info",new_info_value},
    };
    QJsonDocument doc(json2);
    QByteArray data = doc.toJson(QJsonDocument::Compact);
    //qDebug()<<data;
    QNetworkReply *reply = manager.post(request, data);
    QMessageBox:: information(this, "更改成功！","请重新登陆！");
    connect(reply, &QNetworkReply::finished, [=](){});
    //ui->messageSendTextEdit->clear();
    this->close();
    LogOnWidget *low=new LogOnWidget(nullptr,"");//连接到登录页面
    low->show();
}
void changepersonalinfo::on_btn_return_clicked()//返回friendlist页面
{
    this->close();
    friendlist* w1 = new friendlist(nullptr,userSelf);
    w1->show();
}
void changepersonalinfo::on_btn_cancel_clicked()//清空填的内容
{
    ui->reg_username_lineEdit->clear();
    ui->reg_userbirthday_lineEdit->clear();
    ui->reg_useremail_lineEdit->clear();
    ui->reg_userphoto_lineEdit->clear();
    ui->reg_userqq_lineEdit->clear();
}
void changepersonalinfo::slots_gender()
{
    if(groupButton1->checkedId()==0)
        gender="0";
    else
        gender="1";
}
